#include "curlRequest.h"

int sendGet(char *url, char *param, size_t (*f)(void *, size_t, size_t, void *))
{
    // 初始化
    CURL *curl = curl_easy_init();
    if (curl)
    {
        // 设置相关参数  url  post数据 设置响应回调
        char *urlTmp = calloc(100, sizeof(char));
        sprintf(urlTmp, "%s?%s", url, param);        // 通过字符串格式化得到想要的内容
        curl_easy_setopt(curl, CURLOPT_URL, urlTmp); // url+?+param
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, f);
        // 发请求
        CURLcode res = curl_easy_perform(curl);
        // 判断是否发送成功
        if (res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            return -1;
        }
        // 清理
        free(urlTmp);
        curl_easy_cleanup(curl);
    }

    return 0;
}
int sendPost(char *url, char *param, size_t (*f)(void *, size_t, size_t, void *))
{
    // 初始化
    CURL *curl = curl_easy_init();
    if (curl)
    {
        // 设置相关参数  url  post数据 设置响应回调
        curl_easy_setopt(curl, CURLOPT_URL, url); // url+?+param
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, f);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, param); // 设置post的数据
        // 发请求
        CURLcode res = curl_easy_perform(curl);

        // 判断是否发送成功
        if (res != CURLE_OK)
        {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
            return -1;
        }
        // 清理
        curl_easy_cleanup(curl);
    }
    return 0;
}